package com.lightsh.network;

import java.util.ArrayList;
import java.util.Locale;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;

public class HttpHeaderConstants {
	
	public static final String COOKIE_RANGE = "Range";
	public static final String RANGE_VALUE_FORMAT = "bytes=%d-";
	
	public static final String COOKIE_AUTHORIZATION = "Authorization";

	public static final String COOKIE_LANGUAGE = "Accept-Language";
    public static final String COOKIE_NETWORK = "netType";
	public static final String COOKIE_FIRMWARE = "firmware";
	public static final String COOKIE_LOCATION_DATA = "locationData";
	public static final String COOKIE_IMEI = "imei";
	public static final String COOKIE_SN = "sn";
	public static final String COOKIE_SYSTEM_VERSION = "M-System-Version";
	public static String COOKIE_APP_VERSION = "App-Version";
	
	private static final String NETWORK_TYPE_WIFI = "wifi";
	private static final String NETWORK_TYPE_2G = "2g";
	private static final String NETWORK_TYPE_3G = "3g";
	private static final String NETWORK_TYPE_NONE = "none";
	
	public static final String getLanguage(){
		return Locale.getDefault().getLanguage().toLowerCase() + "-" + Locale.getDefault().getCountry().toLowerCase();
	}
	
	public static final String getNetworkType(Context context){
		try{
			ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(connectivity != null){
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if(info != null && info.isAvailable()){
					if(info.getType() == ConnectivityManager.TYPE_WIFI){
						return NETWORK_TYPE_WIFI;
					}else{
						TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
						if(tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS || tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE){
							return NETWORK_TYPE_2G;
						}else{
							return NETWORK_TYPE_3G;
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return NETWORK_TYPE_NONE;
	}
	
	public static final String getLocationData(Context context){
		try{
			TelephonyManager teleManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
			GsmCellLocation gcl = (GsmCellLocation)teleManager.getCellLocation();		
			if(gcl != null){
				int cid = gcl.getCid();
				int lac = gcl.getLac();
				ArrayList<NeighboringCellInfo> neighCelInfoList = new ArrayList<NeighboringCellInfo>();
				neighCelInfoList = (ArrayList<NeighboringCellInfo>) teleManager.getNeighboringCellInfo();
				int mcc = Integer.valueOf(teleManager.getNetworkOperator().substring(0, 3));
				int mnc = Integer.valueOf(teleManager.getNetworkOperator().substring(3, 5));
				
				StringBuilder builder = new StringBuilder();
				builder.append(cid).append(",").append(lac).append(",").append(mcc).append(",").append(mnc);
				
				final int MAX_LENGTH = 5;				
				for( int i = 0; i < neighCelInfoList.size() && i < MAX_LENGTH; ++i ){				
					builder.append(";").append(neighCelInfoList.get(i).getCid()).append(",").append(neighCelInfoList.get(i).getLac()).append(",")
						.append(mcc).append(",").append(mnc);
				}			
				return builder.toString();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
}
